package org.charles.zhifou.common.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <h1>阿里云对象存储配置</h1>
 *
 * @author charles
 * @version 1.0
 */
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
public class AliyunOssConfig {

    private String accessKeyId;

    private String accessKeySecret;

    private String endpoint;

    private String bucketName;

    @Bean
    public OSS ossClient() {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 如果桶不存在，则创建
        if (!ossClient.doesBucketExist(bucketName)) {
            ossClient.createBucket(bucketName);
        }

        return ossClient;
    }
}
